package com.mytest.rabbitMQ.Third;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class ReceiveLogs {
    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 创建fanout类型的交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        // 创建一个非持久的、排他的、自动删除的队列
        String queueName = channel.queueDeclare().getQueue();
        System.out.println("自动生成的队列名称:" + queueName);
        //channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null);   // 之前教程创建的持久化队列
        // 交换机和队列绑定
        channel.queueBind(queueName, EXCHANGE_NAME, "");

        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag ->{});
    }
}
